package xin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import xin.entity.TOrgDepartment;
import xin.dao.TOrgDepartmentMapper;
import xin.entity.TOrgEmployee;
import xin.handler.MyException;
import xin.service.TOrgDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import xin.service.TOrgEmployeeService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author young
 * @since 2023年10月09日
 */
@Service
public class TOrgDepartmentServiceImpl extends ServiceImpl<TOrgDepartmentMapper, TOrgDepartment> implements TOrgDepartmentService {
    @Resource
    private TOrgEmployeeService employeeService;
    @Override
    public Page<TOrgDepartment> findAll(String keyWorld,Integer page,Integer size) {
        Page<TOrgDepartment> pages = new Page<>(page,size);
        QueryWrapper<TOrgDepartment> queryWrapper = new QueryWrapper<>();;
        if (keyWorld!=null){
            queryWrapper.like("name",keyWorld);
        }
        Page<TOrgDepartment> tOrgDepartmentPage = this.baseMapper.selectPage(pages, queryWrapper);
        //设置子级
        Map<Long, TOrgDepartment> collect = tOrgDepartmentPage.getRecords().stream().collect(Collectors.toMap(item -> item.getId(), item -> item));

        collect.forEach((key,value)->{
            if (value.getParentId()!=null){
                //设置上级部门的名称
                if (collect.get(value)!=null)
                value.setParentName(collect.get(value.getParentId()).getName());
                //获取上级部门
                TOrgDepartment tOrgDepartment = collect.get(value.getParentId());
                if (tOrgDepartment!=null)
                if (tOrgDepartment.getChildren()==null) tOrgDepartment.setChildren(new ArrayList<>());
                //添加子部门
                if (tOrgDepartment!=null)
                tOrgDepartment.getChildren().add(value);
            }
        });
        //查找子部门
        return tOrgDepartmentPage;
    }

    @Override
    public Integer add(TOrgDepartment department) {

        return this.save(department)?1:0;
    }

    @Override
    public Integer update(TOrgDepartment department) {
        return this.baseMapper.updateById(department);
    }

    @Override
    public Integer delete(List<Long> ids) {

        //查看该部门下是否含有子部门
        List<TOrgDepartment> parent_id = this.baseMapper.selectList(new QueryWrapper<TOrgDepartment>().in("parent_id", ids));

        if (parent_id!=null&&parent_id.size()>=1){
            throw new MyException("部门下面含有子部门无法删除");
        }
        //查看部门下是否有员工
        List<TOrgEmployee> department_id = this.employeeService.getBaseMapper().selectList(new QueryWrapper<TOrgEmployee>().in("department_id", ids));

        if (department_id!=null&&department_id.size()>=1)
            throw new MyException("部门下含有员工信息，无法删除");

        //删除部门
        int i = this.baseMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public TOrgDepartment findById(Long id) {

        TOrgDepartment tOrgDepartment = this.baseMapper.selectById(id);
        return tOrgDepartment;

    }
}
